﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace CCRPlanner {
    public class Room : INotifyPropertyChanged {
        public Int32 Day { get; set; }
        public Int32 RoomNumber { get; set; }
        public Int32 TimeBlocks { get; set; }
        public ObservableCollection<SessionStats> Sessions { get; set; }

        public String RoomName {
            get {
                return String.Format("Day {0}, Room {1}", Day, RoomNumber);
            }
        }

        public Int32 Conflicts {
            get {
                return Sessions.Sum(x => x.Conflicts);
            }
        }

        public Boolean TimeRemaining {
            get {
                Int32 used = Sessions.Sum(x => x.BlockLength);
                if (used <= TimeBlocks)
                    return true;
                return false;
            }
        }

        public Int32 TimeAvail {
            get {
                Int32 used = Sessions.Sum(x => x.BlockLength);
                return TimeBlocks - used;
            }
        }

        public Boolean HaveTime(SessionStats ses) {
            Int32 used = Sessions.Sum(x => x.BlockLength);
            if (used + ses.BlockLength <= TimeBlocks)
                return true;
            return false;
        }

        public Room() {
            Sessions = new ObservableCollection<SessionStats>();
            Sessions.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Sessions_CollectionChanged);
        }

        public SessionStats GetSessionByBlock(Int32 Block) {
            Int32 curBlock = 0;
            foreach (SessionStats ses in this.Sessions) {
                curBlock += ses.BlockLength;
                if (curBlock >= Block)
                    return ses;
            }

            return null;
        }

        void Sessions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
            Int32 curBlock = 1;
            foreach (SessionStats s in Sessions) {
                s.Block = curBlock;
                curBlock += s.BlockLength;
            }
            RaisePropertyChanged("TimeAvail");
            RaisePropertyChanged("Conflicts");
            RaisePropertyChanged("Sessions");
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(String prop) {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }
}
